package com.hyh.admin.controller.resources;

import com.hyh.ad.common.core.controller.BaseController;
import com.hyh.ad.common.core.domain.AjaxResult;
import com.hyh.ad.common.core.page.TableDataInfo;
import com.hyh.ad.common.utils.SecurityUtils;
import com.hyh.admin.domain.resources.ResourcesCommunity;
import com.hyh.admin.domain.resources.vo.CommunityVo;
import com.hyh.admin.service.ResourcesCommunityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/resources-community")
@Api(tags = "资源社区管理")
public class ResourcesCommunityController extends BaseController {

    @Autowired
    private ResourcesCommunityService resourcesCommunityService;

    /**
     * 添加资源
     *
     * @param resourcesCommunity 资源信息
     * @return 是否成功
     */
    @PostMapping("/add")
    @ApiOperation(value = "添加资源")
    public AjaxResult saveResourcesCommunity(@RequestBody ResourcesCommunity resourcesCommunity) {
        resourcesCommunity.setUserId(SecurityUtils.getLoginUser().getUser().getId());
        return toAjax(resourcesCommunityService.saveResourcesCommunity(resourcesCommunity));
    }

    /**
     * 更新资源
     *
     * @param resourcesCommunity 资源信息
     * @return 是否成功
     */
    @PutMapping("/update")
    @ApiOperation(value = "更新资源")
    public AjaxResult updateResourcesCommunity(@RequestBody ResourcesCommunity resourcesCommunity) {
        resourcesCommunity.setUserId(SecurityUtils.getLoginUser().getUser().getId());
        return toAjax(resourcesCommunityService.updateResourcesCommunity(resourcesCommunity));
    }

    /**
     * 删除资源
     *
     * @param id 资源ID
     * @return 是否成功
     */
    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除资源")
    public AjaxResult deleteResourcesCommunity(@PathVariable Long id) {
         return AjaxResult.success(resourcesCommunityService.deleteResourcesCommunity(id));
    }

    /**
     * 根据ID查询资源
     *
     * @param id 资源ID
     * @return 资源信息
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据ID查询资源")
    public AjaxResult getResourcesCommunityById(@PathVariable Long id) {
        ResourcesCommunity resourcesCommunity = resourcesCommunityService.getResourcesCommunityById(id);
        return AjaxResult.success(resourcesCommunity);
    }

    /**
     * 查询所有资源
     *
     * @return 资源列表
     */
    @GetMapping("/list")
    public AjaxResult getAllResourcesCommunity(ResourcesCommunity resourcesCommunity) {
        List<ResourcesCommunity> resourcesList = resourcesCommunityService.getAllResourcesCommunity(resourcesCommunity);
        return AjaxResult.success(resourcesList);
    }

    /**
     * 分页查询资源
     *
     * @return 资源列表
     */
    @GetMapping("/page")
    @ApiOperation(value = "分页查询资源")
    @PreAuthorize("hasAnyAuthority('sys:admin:permission')")
    public TableDataInfo getResourcesCommunityPage(ResourcesCommunity resourcesCommunity) {
        startPage();
        List<ResourcesCommunity> resourcesList = resourcesCommunityService.getAllResourcesCommunity(resourcesCommunity);
        return getDataTable(resourcesList);
    }


    /*
     * 查询所有帖子 包括用户列表
     */
    @GetMapping("/all")
    @ApiOperation(value = "查询所有帖子 --包括用户列表")
    public AjaxResult getAllResourcesCommunityWithUser() {
        List<CommunityVo> resourcesList = resourcesCommunityService.getAllResourcesCommunityIncludeUsers();
        return AjaxResult.success(resourcesList);
    }

}
